<?php
require_once "db.php";
session_start();

$content = "<div class='home-column'>
		<div class='column-title'>
		<span>Premieres</span>
		</div>";

$premieres_query_text = "SELECT movie_name, picture_url, description FROM movies WHERE past_present_future = 4 ORDER BY RAND() LIMIT 3";
$premieres_query = db_query($premieres_query_text);
while ($row = mysqli_fetch_assoc($premieres_query)) {
	$cut_descr = substr($row['description'], 0, 350)."...";
	$content .= "<div class='movie-cell'>
			<div class='movie-cell-title'>
				<a href='#'>".$row['movie_name']."</a>
			</div>
			<div class='movie-cell-cover'>
				<a href='#'><img src='movie-covers".$row['picture_url']."'/></a>
			</div>
			<div class='movie-cell-descr'>".$cut_descr."</div>
			<div class='movie-cell-more'> <a href='#'> More</a>
			</div>
		</div>";
}


$content .= "</div>
		<div class='home-column'>
		<div class='column-title'>
		<span>Top rated</span>
		</div>";
$top_rated_query_text = "SELECT movie_name, picture_url, description FROM movies WHERE past_present_future = 1 ORDER BY overal_rating DESC LIMIT 3";
$top_rated_query = db_query($top_rated_query_text);
while ($row = mysqli_fetch_assoc($top_rated_query)) {
	$cut_descr = substr($row['description'], 0, 350)."...";
	$content .= "<div class='movie-cell'>
			<div class='movie-cell-title'>
				<a href='#'>".$row['movie_name']."</a>
			</div>
			<div class='movie-cell-cover'>
				<a href='#'><img src='movie-covers".$row['picture_url']."'/></a>
			</div>
			<div class='movie-cell-descr'>".$cut_descr."</div>
			<div class='movie-cell-more'> <a href='#'> More</a>
			</div>
		</div>";
}

$content .= "</div>
		<div class='home-column'>
		<div class='column-title'>
		<span>What is comming</span>
		</div>";
$next_query_text = "SELECT movie_name, picture_url, description FROM movies WHERE past_present_future = 2 ORDER BY RAND() LIMIT 3";
$next_query = db_query($next_query_text);
while ($row = mysqli_fetch_assoc($next_query)) {
	$cut_descr = substr($row['description'], 0, 350)."...";
	$content .= "<div class='movie-cell'>
			<div class='movie-cell-title'>
				<a href='#'>".$row['movie_name']."</a>
			</div>
			<div class='movie-cell-cover'>
				<a href='#'><img src='movie-covers".$row['picture_url']."'/></a>
			</div>
			<div class='movie-cell-descr'>".$cut_descr."</div>
			<div class='movie-cell-more'> <a href='#'> More</a>
			</div>
		</div>";
}

if (isset($_SESSION['uid'])) {
	$content .= "</div>
		<div class='home-column'>
		<div class='column-title'>
		<span>Just for you</span>
		</div>";
	$ufav1 = $_SESSION['ufav1'];
	$ufav2 = $_SESSION['ufav2'];
	$ufav3 = $_SESSION['ufav3'];
	$fav_query_text = "SELECT movie_name, picture_url, description FROM movies 
						WHERE (past_present_future = 1 OR past_present_future = 4) 
						AND ((m_genre1 = '$ufav1' OR m_genre1 = '$ufav2' OR m_genre1 = '$ufav3') 
							OR (m_genre2 = '$ufav1' OR m_genre2 = '$ufav2' OR m_genre2 = '$ufav3') 
							OR (m_genre3 = '$ufav1' OR m_genre3 = '$ufav2' OR m_genre3 = '$ufav3')) 
						ORDER BY RAND() LIMIT 3";
    $fav_query = db_query($fav_query_text);
    while ($row = mysqli_fetch_assoc($fav_query)) {
	$cut_descr = substr($row['description'], 0, 350)."...";
	$content .= "<div class='movie-cell'>
			<div class='movie-cell-title'>
				<a href='#'>".$row['movie_name']."</a>
			</div>
			<div class='movie-cell-cover'>
				<a href='#'><img src='movie-covers".$row['picture_url']."'/></a>
			</div>
			<div class='movie-cell-descr'>".$cut_descr."</div>
			<div class='movie-cell-more'> <a href='#'> More</a>
			</div>
		</div>";
	}
}

$content .= "</div>";
echo $content;

